import 'package:fish_redux/fish_redux.dart';
import 'package:school_safe_teacher/attendanceRecord/entity/attendanceRecordEntity.dart';
import 'package:school_safe_teacher/constans/constants.dart';

import 'action.dart';
import 'state.dart';

Reducer<attendanceRecordState> buildReducer() {
  return asReducer(
    <Object, Reducer<attendanceRecordState>>{
      attendanceRecordAction.action: _onAction,
      attendanceRecordAction.onSuccess:_onSuccess,
    },
  );
}

attendanceRecordState _onAction(attendanceRecordState state, Action action) {
  final attendanceRecordState newState = state.clone();
  return newState;
}
attendanceRecordState _onSuccess(attendanceRecordState state,Action action) {
  final attendanceRecordState newState = state.clone();
  List<attendanceRecordEntity> list = action.payload;
  print(list.length.toString());
  if (list.length < Constants.PAGE_SIZE) {
    print("111");
    state.easyRefreshController.finishLoad(noMore: true);
  }
  if (newState.page == 1) {
    print("222");
    newState..list = list;
    state.easyRefreshController.finishRefresh();
  } else {
    print("333");
    newState.list.addAll(list);
    state.easyRefreshController.finishLoad(noMore:list.length!=Constants.PAGE_SIZE);
  }
  return newState;
}